package util;


import android.media.ExifInterface;

import org.json.JSONObject;

import java.io.IOException;

/**
 * Created by guangdye on 2014/10/4.
 */
public class LocationUtil {


    public static String getLatitude(String path) {
        String latitude = "";
//        ExifInterface exif = new ExifInterface(allImageList.get(i).imagePath);
        try {
            ExifInterface exif = new ExifInterface(path);
            if (exif.getAttribute(ExifInterface.TAG_GPS_LATITUDE) != null && exif.getAttribute(ExifInterface.TAG_GPS_LONGITUDE) != null) {
                String s_lat = exif.getAttribute(ExifInterface.TAG_GPS_LATITUDE);
                String s_lon = exif.getAttribute(ExifInterface.TAG_GPS_LONGITUDE);

                Double s1_la1 = Double.parseDouble(s_lat.split(",")[0].trim().split("/")[0]);
                Double s1_lo1 = Double.parseDouble(s_lat.split(",")[1].trim().split("/")[0]);
                Double s1_lo2 = Double.parseDouble(s_lat.split(",")[1].trim().split("/")[1]);

                Double s2_la1 = Double.parseDouble(s_lon.split(",")[0].trim().split("/")[0]);
                Double s2_lo1 = Double.parseDouble(s_lon.split(",")[1].trim().split("/")[0]);
                Double s2_lo2 = Double.parseDouble(s_lon.split(",")[1].trim().split("/")[1]);

                String latitude2 = String.valueOf(s1_la1 + (s1_lo1 / s1_lo2 / 60));


                latitude = s_lat == null ? "1000" : latitude2;

            }


        } catch (IOException e) {
            e.printStackTrace();
        }
        return latitude;
    }


    public static String getLongitude(String path) {
        String longitude = "";
        try {
            ExifInterface exif = new ExifInterface(path);
            if (exif.getAttribute(ExifInterface.TAG_GPS_LATITUDE) != null && exif.getAttribute(ExifInterface.TAG_GPS_LONGITUDE) != null) {
                String s_lat = exif.getAttribute(ExifInterface.TAG_GPS_LATITUDE);
                String s_lon = exif.getAttribute(ExifInterface.TAG_GPS_LONGITUDE);

                Double s1_la1 = Double.parseDouble(s_lat.split(",")[0].trim().split("/")[0]);
                Double s1_lo1 = Double.parseDouble(s_lat.split(",")[1].trim().split("/")[0]);
                Double s1_lo2 = Double.parseDouble(s_lat.split(",")[1].trim().split("/")[1]);

                Double s2_la1 = Double.parseDouble(s_lon.split(",")[0].trim().split("/")[0]);
                Double s2_lo1 = Double.parseDouble(s_lon.split(",")[1].trim().split("/")[0]);
                Double s2_lo2 = Double.parseDouble(s_lon.split(",")[1].trim().split("/")[1]);

                String longitude2 = String.valueOf(s2_la1 + (s2_lo1 / s2_lo2 / 60));

                longitude = s_lon == null ? "1000" : longitude2;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return longitude;
    }

    static JSONObject locationParam;
//
//    public static JSONObject createJSONObject(String lat, String lon) {
//        try {
//            locationParam = new JSONObject();
//            if (lat.equals("") || lon.equals("")) {
//                locationParam.put("country", "");
//                locationParam.put("province", "");
//                locationParam.put("city", "");
//                locationParam.put("district", "");
//                locationParam.put("street", "");
//            } else {
//                getAddress(lat,lon);
//            }
//            if (!lat.equals("") && !lon.equals("")) {
//                //getAddress(lat,lon);
//            }
//
//        } catch (JSONException e) {
//
//            e.printStackTrace();
//        }
//        return locationParam;
//    }

    //获取地址
//    public static void getAddress(String lat, String lon) throws JSONException {
//        Geocoder geocoder = new Geocoder(AppContext.getAppContext());
//        try {
//            List<Address> addresses = geocoder.getFromLocation(Double.parseDouble(lat), Double.parseDouble(lon), 1);
//
//            if (addresses.size() > 0) {
//                Address address = addresses.get(0);
//                String countryCode = address.getCountryCode();
//                String city = address.getLocality() == null ? "" : address.getLocality();
//                String street = address.getThoroughfare() == null ? "" : address.getThoroughfare();
//                String district = address.getSubLocality() == null ? "" : address.getSubLocality();
//                String province = address.getAdminArea() == null ? "" : address.getAdminArea();
//
//                locationParam.put("country", "中国");
//                locationParam.put("province", province);
//                locationParam.put("city", city);
//                locationParam.put("district", district);
//                locationParam.put("street", street);
//            }
//
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }


}
